
package com.wyzz.hr.action;

import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpRewardBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empreward;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class EmpReward extends BaseDownloadAction {

	public EmpReward() {
		erTypeMap = ComonBeans.getValuesToMap(Empreward.PROP_ER_TYPE,
				new boolean[] { true });
		erFormMap = ComonBeans.getSecondLevelSelect(erTypeMap,
				Empreward.PROP_ER_FORM, new boolean[] { true });
		page = new Pager();
		outputIoName = "OEmpReward";
		searchOrExport = null;
	}

	public String list() throws Exception {
		rewardList = searchRewardWithEmp();
		if ("export".equals(searchOrExport))
			return downloadToExcel(rewardList, outputIoName, outmatchModelId,
					"employee");
		else
			return "success";
	}

	private List searchRewardWithEmp() {
		DetachedCriteria dcReward = searchRewardWithEmp_DC();
		addCriteria(dcReward);
		IEmpRewardBo empRewardBo = (IEmpRewardBo) getBean("empRewardBo");
		return empRewardBo.findReward(dcReward, page, searchOrExport);
	}

	private DetachedCriteria searchRewardWithEmp_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empreward.class);
		dc.createAlias(Empreward.PROP_EMPLOYEE, "emp", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "emp.empDeptNo", 1);
		dc.createAlias(Empreward.PROP_DEPARTMENT, "department", 1);
		dc.createAlias(Empreward.PROP_ER_PB_NO, "erPbNo", 1);
		if ("export".equals(searchOrExport)) {
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_PB_NO).toString(),
					Employee.PROP_EMP_PB_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).toString(),
					Employee.PROP_EMP_LOCATION_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_TYPE).toString(), Employee.PROP_EMP_TYPE,
					1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_BENEFIT_TYPE).toString(),
					Employee.PROP_EMP_BENEFIT_TYPE, 1);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 1);
		addOrders(dc, page,
				new String[] {
						(new StringBuilder())
								.append(Empreward.PROP_ER_EXE_DATE).append(
										"-down").toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
		BaseCrit.addEmpDC(dc, "emp", employeeId);
		BaseCrit.addDeptDC(dc, Empreward.PROP_DEPARTMENT,
				Empreward.PROP_ER_PB_NO, null, new Department(departmentId));
		BaseCrit.addDC(dc, Empreward.PROP_ER_TYPE, "eq",
				new String[] { erType });
		BaseCrit.addDC(dc, Empreward.PROP_ER_FORM, "eq",
				new String[] { erForm });
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, new Department(deptId));
		BaseCrit.addDC(dc, Empreward.PROP_ER_CONTENT, "like",
				new String[] { erContent });
		BaseCrit.addDCDateRange(dc, Empreward.PROP_ER_EXE_DATE, erExeDate,
				erExeDateTo);
	}

	public String addReward() throws Exception {
		String currentEmpNo = getCurrentEmpNo();
		if (StringUtils.isEmpty(erType) || StringUtils.isEmpty(erForm)
				|| !erTypeMap.containsKey(erType)) {
			addActionError("\u975E\u6CD5\u53C2\u6570");
			return "error";
		}
		if (employeeId == null || "".equals(employeeId))
			employeeId = currentEmpNo;
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u65B0\u589E\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		}
		Empreward empreward = new Empreward();
		if (StringUtils.isNotEmpty(erPbNo))
			empreward.setErPbNo(new PositionBase(erPbNo));
		empreward.setDepartment(new Department(departmentId));
		empreward.setEmployee(new Employee(employeeId));
		empreward.setErPbNo(new PositionBase(erPbId));
		empreward.setErContent(erContent);
		empreward.setErExeDate(erExeDate);
		empreward.setErForm(erForm);
		empreward.setErType(erType);
		empreward.setErCreateBy(currentEmpNo);
		empreward.setErCreateDate(new Date());
		empreward.setErLastChangeBy(currentEmpNo);
		empreward.setErLastChangeTime(new Date());
		IEmpRewardBo empRewardBo = (IEmpRewardBo) getBean("empRewardBo");
		empRewardBo.insert(empreward);
		addActionMessage("\u65B0\u589E\u5458\u5DE5\u5956\u60E9\u4FE1\u606F\u6210\u529F\u3002");
		return "success";
	}

	public String deleteReward(String etfId) {
		String auth = DWRUtil.checkAuth("EmpReward", "deleteReward");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		IEmpRewardBo empRewardBo = (IEmpRewardBo) getBean("empRewardBo");
		Empreward etf = empRewardBo.getById(etfId);
		if (etf == null)
			return "noId";
		if ("SUB".equals(auth) && !checkAuth(etf.getEmployee().getId())) {
			return "noauth";
		} else {
			empRewardBo.delete(etfId);
			return "success";
		}
	}

	public String updateReward() throws Exception {
		String currentEmpNo = getCurrentEmpNo();
		if (StringUtils.isEmpty(erType) || StringUtils.isEmpty(erForm)
				|| !erTypeMap.containsKey(erType)) {
			addActionError("\u975E\u6CD5\u53C2\u6570");
			return "error";
		}
		if (employeeId == null || "".equals(employeeId))
			employeeId = currentEmpNo;
		if (updateErId == null || "".equals(updateErId))
			return "noId";
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		} else {
			Empreward empreward = new Empreward();
			empreward.setErContent(erContent);
			empreward.setErExeDate(erExeDate);
			empreward.setErType(erType);
			empreward.setErForm(erForm);
			empreward.setErLastChangeBy(currentEmpNo);
			empreward.setErLastChangeTime(new Date());
			IEmpRewardBo empRewardBo = (IEmpRewardBo) getBean("empRewardBo");
			empRewardBo.update(empreward, updateErId);
			addActionMessage("\u66F4\u65B0\u5458\u5DE5\u5956\u60E9\u4FE1\u606F\u6210\u529F\u3002");
			employeeId = null;
			erContent = null;
			erExeDate = null;
			erType = null;
			erForm = null;
			departmentId = null;
			return "success";
		}
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getErPbNo() {
		return erPbNo;
	}

	public void setErPbNo(String erPbNo) {
		this.erPbNo = erPbNo;
	}

	public String getErType() {
		return erType;
	}

	public void setErType(String erType) {
		this.erType = erType;
	}

	public String getErContent() {
		return erContent;
	}

	public void setErContent(String erContent) {
		this.erContent = erContent;
	}

	public Date getErExeDate() {
		return erExeDate;
	}

	public void setErExeDate(Date erExeDate) {
		this.erExeDate = erExeDate;
	}

	public Date getErExeDateTo() {
		return erExeDateTo;
	}

	public void setErExeDateTo(Date erExeDateTo) {
		this.erExeDateTo = erExeDateTo;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public String getUpdateErId() {
		return updateErId;
	}

	public void setUpdateErId(String updateErId) {
		this.updateErId = updateErId;
	}

	public List getRewardList() {
		return rewardList;
	}

	public void setRewardList(List rewardList) {
		this.rewardList = rewardList;
	}

	public String getErForm() {
		return erForm;
	}

	public void setErForm(String erForm) {
		this.erForm = erForm;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public Map getErTypeMap() {
		return erTypeMap;
	}

	public Map getErFormMap() {
		return erFormMap;
	}

	public String getErPbId() {
		return erPbId;
	}

	public void setErPbId(String erPbId) {
		this.erPbId = erPbId;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	private static final long serialVersionUID = 1L;
	private String erPbNo;
	private String erType;
	private Map erTypeMap;
	private String erForm;
	private Map erFormMap;
	private String erContent;
	private Date erExeDate;
	private Date erExeDateTo;
	private String deptId;
	private String deptName;
	private String departmentName;
	private String departmentId;
	private String employeeId;
	private String erPbId;
	private String empNo;
	private String updateErId;
	private List rewardList;
	private Pager page;
	private String outmatchModelId;
	private String outputIoName;
	private String searchOrExport;
}
